package com.yikome.product.crawler;

import java.nio.charset.Charset;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.yikome.product.crawler.api99.dto.Product1688;
import com.yikome.product.crawler.api99.dto.ProductTmallAndTaobao;
import com.yikome.product.crawler.apiyk.dto.ProductPdd;
import com.yikome.product.service.ProductService;
import com.yikome.utils.JSONUtils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CrawlerProductService {
	
	private String apikey99 = "79C33118FD22C5EC6871EB0DF1CC03EC"  ;
	
	private String youkeToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ2YWx1ZSI6InlrenMxMjM0NTZ3ZWJ2YWx1ZSIsIm5hbWUiOiJsdWllcyIsIm5iZiI6MTY0NTY2NTQ4NSwiZXhwIjoxOTYxMTk4Mjg1LCJpc3MiOiIxMzczNzExNThAcXEuY29tIiwiYXVkIjoiMTM3MzcxMTU4QHFxLmNvbSJ9.ADIx6tiuDev36NEyvW6KxXne5CJCMii0OS6jfVlw-rA";
	
	@Autowired
	private ProductService productService ; 
	
	@Async
    public void crawler99TaoBaoAndTmallProduct(Set<String> productUrls) throws InterruptedException {
		log.info("----------------------------CrawlerProductService crawler99Product start----------------------------");
		
		for (String productUrl : productUrls) {
			
			log.info("productUrl : {}" , productUrl);
			//API限制,每秒只能调用一次
			TimeUnit.SECONDS.sleep(2);
			String crawlerJson = HttpRequest.get(StrUtil.format("{}&apikey={}", productUrl , apikey99))
					.charset(Charset.forName("UTF-8"))
					.header("Accept-Encoding", "gzip")
					.execute()
					.body();
			
			log.info("crawlerJson : {}" , crawlerJson);
			
			if(StrUtil.equalsIgnoreCase(StrUtil.unWrap(JSONUtils.getJsonNodeString(crawlerJson, "retcode"), '"'), "0000")) {
				
				try {
					ProductTmallAndTaobao crawlerProduct = JSONUtils.parseJson(JSONUtils.getJsonNodeString(crawlerJson, "data"), ProductTmallAndTaobao.class);
					
					String sourceProductUrl = "" ; 
					if(StrUtil.containsIgnoreCase(productUrl, "tmall")) {
						sourceProductUrl = StrUtil.replace(productUrl, "https://api03.6bqb.com/tmall/detail?itemid", "https://detail.tmall.com/item.htm?id");
					}else if(StrUtil.containsIgnoreCase(productUrl, "taobao")) {
						sourceProductUrl = StrUtil.replace(productUrl, "https://api03.6bqb.com/taobao/detail?itemid", "https://item.taobao.com/item.htm?id");
					}
					productService.createTaobaoTmallProduct99api(crawlerProduct , sourceProductUrl);
				}catch (Exception e) {
					e.printStackTrace();
					log.error("productUrl [{}] crawler error : {}" , productUrl , e.getMessage());
				}
			}
		}
		
		
		log.info("----------------------------CrawlerProductService crawler99Product end----------------------------");
    }

	@Async
	public void crawler991688Product(Set<String> productUrls) throws InterruptedException {
		
		log.info("----------------------------CrawlerProductService crawler991688Product start----------------------------");
		
		for (String productUrl : productUrls) {
			
			log.info("productUrl : {}" , productUrl);
			//API限制,每秒只能调用一次
			TimeUnit.SECONDS.sleep(2);
			String crawlerJson = HttpRequest.get(StrUtil.format("{}&apikey={}", productUrl , apikey99))
					.charset(Charset.forName("UTF-8"))
					.header("Accept-Encoding", "gzip")
					.execute()
					.body();
			
			log.info("crawlerJson : {}" , crawlerJson);
			
			if(StrUtil.equalsIgnoreCase(StrUtil.unWrap(JSONUtils.getJsonNodeString(crawlerJson, "retcode"), '"'), "0000")) {
				
				try {
					
					Product1688 crawlerProduct = JSONUtils.parseJson(JSONUtils.getJsonNodeString(crawlerJson, "data"), Product1688.class);
					String sourceProductUrl = StrUtil.format("https://detail.1688.com/offer/{}.html", StrUtil.replace(productUrl, "https://api03.6bqb.com/alibaba/detail?itemid=", ""));
					
					productService.create1688Product99api(crawlerProduct , sourceProductUrl);
				}catch (Exception e) {
					e.printStackTrace();
					log.error("productUrl [{}] crawler error : {}" , productUrl , e.getMessage());
				}
			}
		}
		log.info("----------------------------CrawlerProductService crawler991688Product end----------------------------");
		
	}
	
	@Async
	public void crawlerykpddProduct(Set<String> productUrls) throws InterruptedException {
		
		log.info("----------------------------CrawlerProductService crawlerykpddProduct start----------------------------");
		
		for (String productUrl : productUrls) {
			
			log.info("productUrl : {}" , productUrl);
			//API限制,每秒只能调用一次
			TimeUnit.SECONDS.sleep(2);
			String crawlerJson = HttpRequest.get(StrUtil.format("{}&parm.token={}", productUrl , youkeToken))
					.charset(Charset.forName("UTF-8"))
					.header("Accept-Encoding", "gzip")
					.execute()
					.body();
			
			log.info("crawlerJson : {}" , crawlerJson);
			
			if(StrUtil.equalsIgnoreCase(StrUtil.unWrap(JSONUtils.getJsonNodeString(crawlerJson, "code"), '"'), "1")) {
				
				try {
					
					ProductPdd crawlerProduct = JSONUtils.parseJson(JSONUtils.getJsonNodeString(crawlerJson, "data"), ProductPdd.class);
					String sourceProductUrl = StrUtil.format("https://yangkeduo.com/goods.html?goods_id={}", StrUtil.replace(productUrl, "http://api.youkezhushou.com/api/Pdd/detail?parm.itemid=", ""));
					
					productService.crawlerykpddProduct(crawlerProduct , sourceProductUrl);
				}catch (Exception e) {
					e.printStackTrace();
					log.error("productUrl [{}] crawler error : {}" , productUrl , e.getMessage());
				}
			}
		}
		log.info("----------------------------CrawlerProductService crawlerykpddProduct end----------------------------");
		
	}
}
